<?php
 jimport('joomla.application.component.view');
 // Load the view framework
 if(!class_exists('VmView'))require(JPATH_COMPONENT_ADMINISTRATOR.DS.'helpers'.DS.'vmview.php');
 jimport('joomla.html.pane');
class svViewDonviquanly extends JView
{
	function display()
	{
		$task = JRequest::getCMD('task');
		$imageurl='';
		$id=0;
		switch ($task)
		{
			case 'add':
				$imageurl='kaka.jpg';
				$this->_edit($id,$imageurl);
				break;
			case 'edit':
				$id=1;
				$this->_edit($id,$imageurl);
				break;
			default :
				// upload hinh anh
				$imageurl=$this->upload();
				$id=JRequest::getInt('id');
				if($id==0)
					{
						$add=JRequest::getVar('add_edit');
						if($add=='true')
							{
								$this->_edit($id,$imageurl);
							}
						else 
					 		{
					 			$this->_default();
							}
					}
				else
					{
						$this->_edit($id,$imageurl);
					}
				break;
		}
		JRequest::setVar( 'hidesubmenu', 1 );
		parent::display();
	}
	// hien thi chi tiet
	function _default()
	{
		jimport('joomla.html.pagination');
		$model=$this->getModel('donviquanly');
		// phan trang
		$total=$model->getTotal();
		//$view=JRequest::getVar('view');
		$view='donviquanly';
		global $mainframe;
		$param['limitstart']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.'.$view.'.limitstart', 'limitstart',0,'int');
		$param['limit']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.'.$view.'.limit', 'limit',$mainframe->getCfg('list_limit'),'int');
		$pager=new JPagination($total, $param['limitstart'], $param['limit']);
		// sap xep
		$param['order']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.'.$view.'.order', 'filter_order','dvql_id','cmd');
		$param['order_Dir']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.'.$view.'.orderDir', 'filter_order_Dir','DESC','word');
		// fill du lieu
		$objectList=$model->getAllDonvi($param);
		$this->assignRef('pager', $pager);
		$this->assignRef('param', $param);
		$this->assignRef('objectList', $objectList);
		//$this->_gridToolbar();
		//toolbar
		$this->toolbarDefault('Quản lý các đơn vị ');
	}
	function _edit($idd,$imageurl)
	{
		// toolbar
		//$this->_formToolbar('đơn vị');
		// lay id tu form default
		if($idd!=0)
			{
				$this->toolbarEdit('Chỉnh sửa đơn vị ');
				if($idd==1)
				{
					$idlist    = JRequest::getVar('cid',array(0),'','array');
					JArrayHelper::toInteger($idlist, array(0));
					$iid     = $idlist[0];
					$id=$iid;
				}
				else
				{
					$id=$idd;
				}
			}
		else
			{
				$this->toolbarAdd('Thêm mới đơn vị ');
				$id=0;
			}
		// lay du lieu tu model
		$model=$this->getModel('donviquanly');
		$object=$model->getDonviquanlyByID($id);
		if($imageurl=='') $imageurl=$object->dvql_hinhanh;
		if($imageurl=='') $imageurl='kaka.jpg';
		$this->assignRef('imageurl', $imageurl);
		// set layout
		$this->assignRef('id', $id);
		$this->assignRef('object', $object);
		$this->setLayout('edit');
	}
	function upload()
	{
		jimport('joomla.client.helper');
		$allowedExts = array("jpg", "jpeg", "gif", "png");
		$extension = end(explode(".", $_FILES["file"]["name"]));
		if ((($_FILES["file"]["type"] == "image/gif")
				|| ($_FILES["file"]["type"] == "image/jpeg")
				|| ($_FILES["file"]["type"] == "image/png")
				|| ($_FILES["file"]["type"] == "image/pjpeg"))
				&& ($_FILES["file"]["size"] < 100000)
				&& in_array($extension, $allowedExts))
		{
			if ($_FILES["file"]["error"] > 0)
			{
				echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
			}
			else
			{
				//echo "Upload: " . $_FILES["file"]["name"] . "<br />";
				//echo "Type: " . $_FILES["file"]["type"] . "<br />";
				//echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
				//echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
				$folder=JPATH_ADMINISTRATOR.DS."components".DS."com_svquanlycanbo".DS."uploads";
				if (file_exists($folder."/" . $_FILES["file"]["name"]))
				{
					echo '<b style="color:red;font-size:18px">Warning : '.$_FILES["file"]["name"] . " already exists. </b>";
					return $_FILES["file"]["name"];
				}
				else
				{
					move_uploaded_file($_FILES["file"]["tmp_name"],
					$folder."/" . $_FILES["file"]["name"]);
					echo '<b style="color:blue;font-size:18px">Upload image successful.! </b>';
					return $_FILES["file"]["name"];
				}
			}
		}
		else
		{
			//echo "Invalid file";
		}
	}
	// them cac button tren thanh toolbar
	function toolbarDefault($title)
	{
		JToolBarHelper::title($title);
		JToolBarHelper::addNew();
		JToolBarHelper::editListX();
		JToolBarHelper::deleteList();
	}
	function toolbarAdd($title)
	{
		JRequest::setVar( 'hidemainmenu', 1 );
		JToolBarHelper::title($title);
		JToolBarHelper::save();
		JToolBarHelper::cancel();
	}
	function toolbarEdit($title)
	{
		JRequest::setVar( 'hidemainmenu', 1 );
		JToolBarHelper::title($title);
		JToolBarHelper::save();
		JToolBarHelper::apply();
		JToolBarHelper::cancel();
	}
	
}
?>